using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace EndNoteHelper.ReferenceEntities
{
    public class Reference
    {
        public Reference()
        {
            _isChecked = false;
        }

        public Reference(string fileName):this("Journal",fileName)
        {
        }

        public Reference(string refType, string fileName)
        {
            FileInfoExtended file = new FileInfoExtended(fileName);
            SetReference(refType,file.FileAuthor,Path.GetFileNameWithoutExtension(file.FileName),
                file.FileCreationDate.Year.ToString(),fileName,file.FileType);
        }

        private void SetReference(string referenceType, string authors, string title, string year,string fullFileName,string refFileType)
        {
            _referenceType = referenceType;
            _authors = authors;
            _title = title;
            _year = year;
            _fullFileName = fullFileName;
            _isChecked = false;
            _fileType = refFileType;
        }

        public string ExportFormatted()
        {
            if (!IsChecked)
            {
                return "";
            }

            StringBuilder sbForReference = new StringBuilder();

            if (_referenceType=="Dissertation/Thesis")
            {
                sbForReference.AppendLine("RT Dissertation/Thesis");
                sbForReference.AppendLine("A1 " + _authors);
                sbForReference.AppendLine("T1 " + _title);
                sbForReference.AppendLine("YR " + _year);
                sbForReference.AppendLine("K1 未知");
                sbForReference.AppendLine("AB 未知");
                sbForReference.AppendLine("DB 未知");
                if (!string.IsNullOrEmpty(_fullFileName))
                {
                    sbForReference.AppendLine("FILE FILE://" + _fullFileName);
                }
                sbForReference.AppendLine();
            }
            else if (_referenceType == "Journal")
            {
                sbForReference.AppendLine("RT Journal");
                sbForReference.AppendLine("A1 " + _authors);
                sbForReference.AppendLine("T1 " + _title);
                sbForReference.AppendLine("JF 未知");
                sbForReference.AppendLine("JO 未知");
                sbForReference.AppendLine("YR " + _year);
                sbForReference.AppendLine("IS 未知");
                sbForReference.AppendLine("K1 未知");
                sbForReference.AppendLine("AB 未知");
                sbForReference.AppendLine("SN 未知");
                sbForReference.AppendLine("CN 未知");
                sbForReference.AppendLine("DB 未知");
                if (!string.IsNullOrEmpty(_fullFileName))
                {
                    sbForReference.AppendLine("FILE FILE://" + _fullFileName);
                }
                
                sbForReference.AppendLine();
            }
            return sbForReference.ToString();
        }

        public string ToRefworksFormat( bool withFile)
        {
            StringBuilder sbForReference = new StringBuilder();

            if (_referenceType == "Dissertation/Thesis")
            {
                sbForReference.AppendLine("RT Dissertation/Thesis");
                sbForReference.AppendLine("A1 " + _authors);
                sbForReference.AppendLine("T1 " + _title);
                sbForReference.AppendLine("YR " + _year);
                sbForReference.AppendLine("K1 "+_keywords);
                sbForReference.AppendLine("AB "+_abstract);
                sbForReference.AppendLine("DB "+_database);
                if (withFile)
                {
                    if (!string.IsNullOrEmpty(_fullFileName))
                    {
                        sbForReference.AppendLine("FILE " + _fullFileName);
                    }
                }
                sbForReference.AppendLine();
            }
            else if (_referenceType == "Journal")
            {
                sbForReference.AppendLine("RT Journal");
                sbForReference.AppendLine("A1 " + _authors);
                sbForReference.AppendLine("T1 " + _title);
                sbForReference.AppendLine("JF "+_journalName);
                sbForReference.AppendLine("JO "+_journalShortName);
                sbForReference.AppendLine("YR " + _year);
                sbForReference.AppendLine("IS "+_issue);
                sbForReference.AppendLine("K1 "+_keywords);
                sbForReference.AppendLine("AB "+_abstract);
                sbForReference.AppendLine("SN "+_sn);
                sbForReference.AppendLine("CN "+_cn);
                sbForReference.AppendLine("DB "+_database);
                if (withFile)
                {
                    if (!string.IsNullOrEmpty(_fullFileName))
                    {
                        sbForReference.AppendLine("FILE " + _fullFileName);
                    }
                }
                sbForReference.AppendLine();
            }
            return sbForReference.ToString();
        }

        public string ReferenceKey
        {
            get
            {
                return _title + _authors + Year;
            }
        }
        public string ReferenceType
        {
            get { return _referenceType; }
            set { _referenceType = value; }
        }

        public string Authors
        {
            get { return _authors; }
            set { _authors = value; }
        }

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public string Year
        {
            get { return _year; }
            set { _year = value; }
        }

        public string Keywords
        {
            get { return _keywords; }
            set { _keywords = value; }
        }

        public string Abstract
        {
            get { return _abstract; }
            set { _abstract = value; }
        }

        public string Database
        {
            get { return _database; }
            set { _database = value; }
        }

        public string FullFileName
        {
            get { return _fullFileName; }
            set { _fullFileName = value; }
        }

        public string FilePath
        {
            get { return Path.GetDirectoryName(_fullFileName) ; }
        }

        public string FileExtention
        {
            get { return Path.GetExtension(_fullFileName) ; }
        }

        public bool IsChecked
        {
            get { return _isChecked; }
            set { _isChecked = value; }
        }

        public string FileType
        {
            get { return _fileType; }
        }

        public string JournalName
        {
            get {
                return _journalName;
            }
            set {
                _journalName = value;
            }
        }

        public string JournalShortName
        {
            get {
                return _journalShortName;
            }
            set {
                _journalShortName = value;
            }
        }

        public string Issue
        {
            get {
                return _issue;
            }
            set {
                _issue = value;
            }
        }

        public string SN
        {
            get {
                return _sn;
            }
            set {
                _sn = value;
            }
        }

        public string CN
        {
            get {
                return _cn;
            }
            set {
                _cn = value;
            }
        }

        public string RefworkFileName
        {
            get {
                return _refworkFileName;
            }
            set {
                _refworkFileName = value;
            }
        }

        public string Op
        {
            get { return _op; }
            set { _op = value; }
        }

        private string _referenceType;
        private string _authors;
        private string _title;
        private string _year;
        private string _keywords;
        private string _abstract;
        private string _database;
        private string _fullFileName;
        private bool _isChecked;
        private string _fileType;
        private string _journalName;
        private string _journalShortName;
        private string _issue;
        private string _op;
        private string _sn;
        private string _cn;
        private string _refworkFileName;
    }
}